﻿Imports BLL
Imports DAL
Public Class Coleta
    Inherits System.Web.UI.Page
    Dim ColCodigo As Integer
    Dim ObjBLL As New ColetaBLL
#Region "Métodos"
    Public Sub carrega()
        Dim tab As DataTable
        tab = ObjBLL.buscar(ColCodigo, 0)
        tdata.Text = FormatDateTime(tab.Rows(0)("col_data").ToString, DateFormat.ShortDate)
        ddlPessoa.SelectedValue = Val(tab.Rows(0)("pes_codigo").ToString)
        TCodigo.Text = ColCodigo

    End Sub
#End Region
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim ObjColeta = New ColetaBLL

        If (Not IsPostBack()) Then
            Dim obj As New Conexao
            obj.preencheCombo(ddlPessoa, "Mul_pessoa", "pes_codigo", "pes_nome", "", "pes_nome")
            obj.preencheCombo(ddlEletronico, "vw_EletronicosJuncao", "ele_codigo", "ele_descricao", "", "ele_descricao")
            obj.preencheCombo(ddlUnidade, "Mul_unidade", "uni_codigo", "uni_descricao", "", "uni_descricao")

            If Not IsNothing(Request("id")) Then
                ColCodigo = Val(Request("id"))
                Session("id") = ColCodigo
                Session("ColCodigo") = ColCodigo
                BGravar.Text = "Alterar"
                carrega()
            ElseIf (Not IsNothing(Session("id"))) Then
                ColCodigo = Val(Session("id"))
                Session("id") = ColCodigo
                Session("ColCodigo") = ColCodigo
                BGravar.Text = "Alterar"
                carrega()
            Else
                BGravar.Text = "Gravar"
            End If


            If ((IsNothing(Session("ColCodigo")))) Then
                'cria coleta
                ColCodigo = ObjColeta.InsereColeta(0, Date.Now, 1, Session("usu_nome"))
                Session("ColCodigo") = ColCodigo
                TCodigo.Text = ColCodigo
            Else
                ColCodigo = Integer.Parse(Session("ColCodigo").ToString())
                TCodigo.Text = ColCodigo
            End If
            If (TCodigo.Text = "") Then
                Session("ColCodigo") = Nothing
                TCodigo.Text = ""
            End If
            gvDados.DataSource = ObjColeta.RetItens(ColCodigo)
            gvDados.DataBind()
          
            ObjColeta = Nothing

        ElseIf ((IsNothing(Session("ColCodigo")))) Then
        Else
            ColCodigo = Integer.Parse(Session("ColCodigo").ToString())

        End If
    End Sub

    Protected Sub gvDados_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvDados.RowDeleting
        Try
            Dim ColCodigo, EleCodigo As Integer

            ColCodigo = Integer.Parse(Session("ColCodigo").ToString())
            EleCodigo = Integer.Parse(gvDados.DataKeys(e.RowIndex).Value.ToString())
            Dim ObjColeta As New ColetaBLL
            ObjColeta.ExcluirItens(ColCodigo, EleCodigo)
            gvDados.DataSource = ObjColeta.RetItens(ColCodigo)
            gvDados.DataBind()
            ObjColeta = Nothing
        Catch ex As Exception
            LMSG.Text = "Ocorreu um erro durante a exclusão."
        End Try
    End Sub

    Protected Sub bAdicionar_Click(sender As Object, e As EventArgs) Handles bAdicionar.Click
        Try
            Dim ObjColeta As New ColetaBLL
            ObjColeta.InsereItens(Integer.Parse(Session("ColCodigo").ToString()), ddlEletronico.SelectedValue, tqtde.Text, ddlUnidade.SelectedValue)
            gvDados.DataSource = ObjColeta.RetItens(Integer.Parse(Session("ColCodigo").ToString()))
            gvDados.DataBind()
            tqtde.Text = ""
            LMSG.Visible = False
        Catch ex As Exception
            LMSG.Text = ex.Message
            LMSG.Visible = True
        End Try

    End Sub

    Protected Sub BGravar_Click(sender As Object, e As EventArgs) Handles BGravar.Click
        Try
            Dim ObjColeta As New ColetaBLL
            ObjColeta.InsereColeta(ColCodigo, tdata.Text, ddlPessoa.SelectedValue, Session("usu_nome"))
            Session("ColCodigo") = Nothing
            Session("id") = Nothing
            LMSG.Text = "Coleta Gravada!"
            LMSG.Visible = True
            BGravar.Enabled = False
            bAdicionar.Enabled = False
        Catch ex As Exception
            LMSG.Text = ex.Message
            LMSG.Visible = True
        End Try
    End Sub

    Protected Sub gvDados_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvDados.PageIndexChanging
        Dim ObjColeta = New ColetaBLL
        gvDados.DataSource = ObjColeta.RetItens(ColCodigo)
        gvDados.DataBind()
        gvDados.PageIndex = e.NewPageIndex
        gvDados.DataBind()
    End Sub

    Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim obj As New Conexao
        obj.preencheCombo(ddlEletronico, "vw_EletronicosJuncao", "ele_codigo", "ele_descricao", "", "ele_descricao")
    End Sub

    Protected Sub ImageButton2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
        Dim obj As New Conexao
        obj.preencheCombo(ddlPessoa, "Mul_pessoa", "pes_codigo", "pes_nome", "", "pes_nome")
    End Sub

    Protected Sub ImageButton3_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton3.Click
        Dim obj As New Conexao
        obj.preencheCombo(ddlUnidade, "Mul_unidade", "uni_codigo", "uni_descricao", "", "uni_descricao")
    End Sub
End Class